<template>
  <div class="timeslot-section">
    <h3 class="section-title">剩余场次时段</h3>
    <div class="timeslot-periods">
      <!-- 上午场次 -->
      <div class="period-group">
        <div class="period-title">上午 09:00-12:00</div>
        <div class="timeslot-grid">
          <div 
            class="timeslot-card"
            :class="{ 
              active: selectedTimeSlot === slot.id, 
              disabled: slot.remaining === 0 || !slot.isAvailable,
              locked: slot.isLocked
            }"
            v-for="slot in morningSlots" 
            :key="slot.id"
            @click="selectTimeSlot(slot)"
          >
            <div class="timeslot-time">{{ slot.time }}</div>
            <div class="timeslot-stock">
              <span v-if="slot.isLocked">已满</span>
              <span v-else-if="!slot.isAvailable">不可预约</span>
              <span v-else>余量：{{ slot.remaining }}/{{ slot.maxCapacity }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 下午场次 -->
      <div class="period-group">
        <div class="period-title">下午 14:00-17:00</div>
        <div class="timeslot-grid">
          <div 
            class="timeslot-card"
            :class="{ 
              active: selectedTimeSlot === slot.id, 
              disabled: slot.remaining === 0 || !slot.isAvailable,
              locked: slot.isLocked
            }"
            v-for="slot in afternoonSlots" 
            :key="slot.id"
            @click="selectTimeSlot(slot)"
          >
            <div class="timeslot-time">{{ slot.time }}</div>
            <div class="timeslot-stock">
              <span v-if="slot.isLocked">已满</span>
              <span v-else-if="!slot.isAvailable">不可预约</span>
              <span v-else>余量：{{ slot.remaining }}/{{ slot.maxCapacity }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 晚上场次 -->
      <div class="period-group">
        <div class="period-title">晚上 17:00-22:00</div>
        <div class="timeslot-grid">
          <div 
            class="timeslot-card"
            :class="{ 
              active: selectedTimeSlot === slot.id, 
              disabled: slot.remaining === 0 || !slot.isAvailable,
              locked: slot.isLocked
            }"
            v-for="slot in eveningSlots" 
            :key="slot.id"
            @click="selectTimeSlot(slot)"
          >
            <div class="timeslot-time">{{ slot.time }}</div>
            <div class="timeslot-stock">
              <span v-if="slot.isLocked">已满</span>
              <span v-else-if="!slot.isAvailable">不可预约</span>
              <span v-else>余量：{{ slot.remaining }}/{{ slot.maxCapacity }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

// Props
const props = defineProps({
  selectedTimeSlot: {
    type: [String, Number],
    default: ''
  },
  timeSlots: {
    type: Array,
    default: () => []
  }
})

// Emits
const emit = defineEmits(['update:selectedTimeSlot', 'timeSlotChanged'])

// 计算属性 - 分组时间段
const morningSlots = computed(() => {
  return props.timeSlots.filter(slot => slot.periodName === '上午场次')
    .sort((a, b) => a.time.localeCompare(b.time))
})

const afternoonSlots = computed(() => {
  return props.timeSlots.filter(slot => slot.periodName === '下午场次')
    .sort((a, b) => a.time.localeCompare(b.time))
})

const eveningSlots = computed(() => {
  return props.timeSlots.filter(slot => slot.periodName === '晚上场次')
    .sort((a, b) => a.time.localeCompare(b.time))
})

// 选择时间段
const selectTimeSlot = (slot) => {
  if (slot.remaining === 0 || !slot.isAvailable) return
  emit('update:selectedTimeSlot', slot.id)
  emit('timeSlotChanged', slot)
}
</script>

<style scoped>
/* 时间段选择区域 */
.timeslot-section {
  background: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

.section-title {
  margin: 0 0 15px 0;
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.timeslot-periods {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.period-group {
  background: linear-gradient(135deg, #fafbff 0%, #f0f4ff 100%);
  border-radius: 12px;
  padding: 18px;
  border: 1px solid rgba(102, 126, 234, 0.1);
  box-shadow: 0 2px 12px rgba(102, 126, 234, 0.05);
}

.period-title {
  font-size: 16px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 16px;
  text-align: center;
  letter-spacing: 0.5px;
  position: relative;
}

.period-title::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 2px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  border-radius: 1px;
}

.timeslot-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.timeslot-card {
  border: 2px solid #e3e8f5;
  border-radius: 10px;
  padding: 14px 10px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background: white;
  min-height: 70px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.05);
}

.timeslot-card.active {
  border-color: #5a6fd8;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3);
  transform: translateY(-2px);
}

.timeslot-card.disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background: #f8f9fa;
  border-color: #e9ecef;
}

.timeslot-card.locked {
  background: linear-gradient(135deg, #ffebee 0%, #fce4ec 100%);
  border-color: #f44336;
  opacity: 0.85;
  cursor: not-allowed;
}

.timeslot-card.locked .timeslot-stock {
  color: #d32f2f;
  font-weight: 700;
}

.timeslot-card:not(.disabled):not(.active):not(.locked):hover {
  border-color: #667eea;
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.15);
  background: #fafbff;
}

.timeslot-time {
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 6px;
  color: #2c3e50;
  line-height: 1.2;
  letter-spacing: 0.3px;
}

.timeslot-card.active .timeslot-time {
  color: white;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.timeslot-card.locked .timeslot-time {
  color: #d32f2f;
  font-weight: 600;
}

.timeslot-card.disabled .timeslot-time {
  color: #adb5bd;
}

.timeslot-stock {
  font-size: 12px;
  font-weight: 600;
  color: #5a6c7d;
  line-height: 1.3;
}

.timeslot-card.active .timeslot-stock {
  color: rgba(255, 255, 255, 0.9);
}

.timeslot-card.disabled .timeslot-stock {
  color: #adb5bd;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .timeslot-periods {
    gap: 15px;
  }
  
  .period-group {
    padding: 16px;
    border-radius: 10px;
    background: linear-gradient(135deg, #f8faff 0%, #f0f3ff 100%);
  }
  
  .period-title {
    font-size: 15px;
    font-weight: 700;
    margin-bottom: 14px;
    color: #2c3e50;
    letter-spacing: 0.3px;
  }
  
  .period-title::after {
    width: 30px;
    bottom: -6px;
  }
  
  .timeslot-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }
  
  .timeslot-card {
    padding: 12px 6px;
    min-height: 65px;
    border-radius: 10px;
    border-width: 1.5px;
  }
  
  .timeslot-card:not(.disabled):not(.active):not(.locked):hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
  }
  
  .timeslot-card.active {
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
    transform: translateY(-1px);
  }
  
  .timeslot-time {
    font-size: 14px;
    font-weight: 700;
    color: #2c3e50;
    margin-bottom: 6px;
    line-height: 1.2;
    letter-spacing: 0.3px;
  }
  
  .timeslot-card.active .timeslot-time {
    color: white;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  }
  
  .timeslot-stock {
    font-size: 11px;
    font-weight: 600;
    color: #5a6c7d;
    line-height: 1.3;
  }
  
  .timeslot-card.active .timeslot-stock {
    color: rgba(255, 255, 255, 0.9);
  }
  
  .timeslot-card.locked .timeslot-time {
    color: #d32f2f;
    font-weight: 600;
  }
  
  .timeslot-card.disabled .timeslot-time {
    color: #bbb;
  }
  
  .timeslot-card.disabled .timeslot-stock {
    color: #ccc;
  }
}
</style> 